package data;

import java.sql.PreparedStatement;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import dataTransferObject.Rol;

public class JDBCRolDAO implements IRolDAO {

	public String selectRol(int idRol) {
		Connection conn = ConnectionManager.getInstance().CheckOut();
		PreparedStatement stmt = null;
        ResultSet result = null;
        String rol = null;
        String sql = "SELECT * FROM rol WHERE (ID_ROL = ? ) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, idRol);

            result = stmt.executeQuery();

            result.next();
            rol = result.getString("NOMBRE");
        } catch (SQLException e) {
            System.out.println("selectRol Message: " + e.getMessage());
            System.out.println("selectRol SQLState: " + e.getSQLState());
            System.out.println("selectRol ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().CheckIn(conn);
            try {
                if (result != null) {
                    result.close();
                }

                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return rol;
	}
	
	public String selectRolPorUsuario(Connection conn,int idUsuario) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        String rol = null;
        String sql = "SELECT * FROM perfil WHERE (ID_USUARIO = ? ) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, idUsuario);

            result = stmt.executeQuery();

            result.next();
            rol = selectRol(result.getInt("ID_ROL"));
            
        } catch (SQLException e) {
            System.out.println("selectRolPorUsuario Message: " + e.getMessage());
            System.out.println("selectRolPorUsuario SQLState: " + e.getSQLState());
            System.out.println("selectRolPorUsuario ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }

                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return rol;
	}

	public List<Rol> selectAllRoles() {
		Connection conn = ConnectionManager.getInstance().CheckOut();
		PreparedStatement stmt = null;
		List<Rol> listResult = new LinkedList<Rol>();
		String sql = "SELECT * FROM rol";
		ResultSet result = null;
		
		try {
			stmt = conn.prepareStatement(sql);
			result = stmt.executeQuery();
			
			while (result.next()) {
				Rol transfer = new Rol();
				transfer.setIdRol(result.getInt("ID_ROL"));
				transfer.setNombre(result.getString("NOMBRE"));
				listResult.add(transfer);
			}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			ConnectionManager.getInstance().CheckIn(conn);
			try {
				if (result != null) {
					result.close();
				}
				if (stmt != null) {
					stmt.close();
				}
			} catch (Exception e) {
			}
		}
		return listResult;
	}
}
